package com.xmad.ksadutils.AdSDK.AdMold.Kwai;

import android.app.Activity;
import android.os.Handler;
import android.util.Log;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsFullScreenVideoAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsRewardVideoAd;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.SdkConfig;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.Interface.AdLoad;
import com.xmad.ksadutils.AdSDK.Utlis.AdComplete_;
import com.xmad.ksadutils.AdSDK.Utlis.Click;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

;

public class KsFullVideoAd extends AdBaseLoad implements AdLoad {
    private volatile static KsFullVideoAd mInstance;
    KsFullScreenVideoAd mFullScreenVideoAd;
    static Map<Long, AdComplete_> mMap= new HashMap<Long,AdComplete_>();
    Activity activity;
    /**
     * 单例模式
     *
     * @return
     */
    public static KsFullVideoAd getInstance() {
        if (mInstance == null) {
            synchronized (KsFullVideoAd.class) {
                if (mInstance == null) {
                    mInstance = new KsFullVideoAd();
                }
            }
        }
        return mInstance;
    }

    @Override
    public void Show(Activity activity, FrameLayout frameLayout) {
        this.activity=activity;
        if (KsAds==null||KsAds.size()==0){
            Log.e(TAG,"先设置梯度ID");
            return;
        }
        //新建
        NewMap();
        //获取广告对象List列表
        GetAdList();


    }
    //新建
    public void NewMap(){
        if (mMap.size()==0){
            for (Long Id:KsAds){
                mMap.put(Id,null);
            }
        }
    }

    //获取广告对象List列表
    public void GetAdList(){
        for(Long key:mMap.keySet()){
            if (mMap.get(key)==null||mMap.get(key).getAd()==null){
                getAds(key);
            }
        }
    }





    private void   getAds(Long id){
        Log.e(TAG,"激励视频————加载————"+id);
        KsScene.Builder builder = new KsScene.Builder(id).screenOrientation(SdkConfig.SCREEN_ORIENTATION_UNKNOWN);
        Map<String, String> rewardCallbackExtraData = new HashMap<>();
        rewardCallbackExtraData.put("thirdUserId", "test-uerid-jia");
        rewardCallbackExtraData.put("extraData", "testExtraData");
        builder.rewardCallbackExtraData(rewardCallbackExtraData);
        KsScene scene = builder.build(); // 此为测试posId，请联系快手平台申请正式posId
        KsAdSDK.getLoadManager().loadFullScreenVideoAd(scene,
                new KsLoadManager.FullScreenVideoAdListener() {
                    @Override
                    public void onError(int code, String msg) {
                        if (listener!=null){
                            listener.onError(code+msg);
                        }
                    }

                    @Override
                    public void onRequestResult(int adNumber) {

                    }

                    @Override
                    public void onFullScreenVideoAdLoad(@Nullable List<KsFullScreenVideoAd> adList) {
                        if (adList != null && adList.size() > 0) {
                            mFullScreenVideoAd = adList.get(0);
//                            showFullScreenVideoAd(activity,null);
                            mMap.put(id,new AdComplete_(true,mFullScreenVideoAd));
                            // 广告展示
                          //  Show();
                        }
                    }
                });
    }

   // 广告展示
    public void start(){
        if (Is_Show()){
            Log.e(TAG,"Is_Show------"); 
            for (Long id:KsAds){
                if (mMap.get(id).getAd()!=null){
                    showFullScreenVideoAd(activity,mMap.get(id).getAd());
                    Log.e(TAG,"id---------"+id);
                    mMap.put(id,null);
                    break;
                }
            }
        }else {
            Log.e(TAG,"需要继续");
        }
    }

    //查询是否进行广告展示
    private boolean Is_Show(){
        boolean Complete = false;
        for (Long key : mMap.keySet()) {
            if (mMap.get(key)!=null){
                if (mMap.get(key).isComplete()){
                    Complete=mMap.get(key).isComplete();
                }else {
                    return false;
                }
            }else {
                return false;
            }
        }
        return Complete;
    }


    private void showFullScreenVideoAd(Activity activity,final KsFullScreenVideoAd mFullScreenVideoAd) {
        if (mFullScreenVideoAd != null && mFullScreenVideoAd.isAdEnable()) {
            mFullScreenVideoAd
                    .setFullScreenVideoAdInteractionListener(new KsFullScreenVideoAd.FullScreenVideoAdInteractionListener() {
                        @Override
                        public void onAdClicked() {
                            //ToastUtil.showToast(mContext, "全屏视频广告点击");
                            Log.e(TAG,"激励视频广告点击");
                            Click.Jump=true;
                        }

                        @Override
                        public void onPageDismiss() {
                            //ToastUtil.showToast(mContext, "全屏视频广告关闭");
                            Log.e(TAG,"激励视频广告关闭");
                            if (listener!=null){
                                listener.onPageDismiss();
                            }
                        }

                        @Override
                        public void onVideoPlayError(int code, int extra) {
                            //ToastUtil.showToast(mContext, "全屏视频广告播放出错");
                        }

                        @Override
                        public void onVideoPlayEnd() {
                            // ToastUtil.showToast(mContext, "全屏视频广告播放完成");
                        }

                        @Override
                        public void onVideoPlayStart() {
                            // ToastUtil.showToast(mContext, "全屏视频广告播放开始");
                            Log.e(TAG,"激励视频广告播放开始");
                            if (listener!=null){
                                listener.onAdShow();

                            }
                        }

                        @Override
                        public void onSkippedVideo() {
                            //ToastUtil.showToast(mContext, "全屏视频广告播放跳过");

                        }
                    });
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mFullScreenVideoAd.showFullScreenVideoAd(activity, null);
                }
            },3000);
        } else {
            //ToastUtil.showToast(mContext, "暂无可用全屏视频广告，请等待缓存加载或者重新刷新");
            Log.e(TAG,"暂无可用全屏视频广告");
        }
    }



    private void showRewardVideoAd(Activity activity,KsRewardVideoAd mRewardVideoAd) {
        if (mRewardVideoAd != null && mRewardVideoAd.isAdEnable()) {
            mRewardVideoAd.setRewardAdInteractionListener(new KsRewardVideoAd.RewardAdInteractionListener() {
                @Override
                public void onAdClicked() {
                    Log.e(TAG,"激励视频广告点击");
                    Click.Jump=true;
                }

                @Override
                public void onPageDismiss() {
                    Log.e(TAG,"激励视频广告关闭");
                    if (listener!=null){
                        listener.onPageDismiss();
                    }
                }

                @Override
                public void onVideoPlayError(int code, int extra) {
                    Log.e(TAG,"激励视频广告播放出错");
                }

                @Override
                public void onVideoPlayEnd() {
                    Log.e(TAG,"激励视频广告播放完成");
                }

                @Override
                public void onVideoSkipToEnd(long l) {

                }

                //                @Override
//                public void onVideoSkipToEnd(long l) {
//
//                }
                @Override
                public void onVideoPlayStart() {
                    Log.e(TAG,"激励视频广告播放开始");
                    if (listener!=null){
                        listener.onAdShow();

                    }
                }
                @Override
                public void onRewardVerify() {
                    Log.e(TAG,"激励视频广告获取激励");
                    if (listener!=null){
                        listener.onRewardVerify();
                    }
                }


            });

            mRewardVideoAd.showRewardVideoAd(activity, null);
         } else {
            Log.e(TAG,"暂无可用激励视频广告，请等待缓存加载或者重新刷新");
         }
    }







}
